package com.jewaton.auth.commons.service.impl;

import com.jewaton.auth.commons.dao.OrganizationDAO;
import com.jewaton.auth.commons.entity.District;
import com.jewaton.auth.commons.entity.Organization;
import com.jewaton.auth.commons.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织表service实现类
 */
@Service(value = "organizationService")
public class OrganizationServiceImpl  implements OrganizationService {

	@Autowired
	private OrganizationDAO organizationDAO;

	@Override
	public Organization addOrganization(Organization organization) {
		organizationDAO.save(organization);
		return organization;
	}

	@Override
	public Organization updateOrganization(Organization organization) {
		organizationDAO.update(organization);
		return organization;
	}

	@Override
	public Organization deleteOrganization(Organization organization) {
		organizationDAO.delete(organization);
		return null;
	}


	public void setOrganizationDAO(OrganizationDAO organizationDAO) {
		this.organizationDAO = organizationDAO;
	}

    @Override
    public Organization findOrganizationById(String oid) {
        // TODO Auto-generated method stub
        return organizationDAO.findOrganizationById(oid);
    }

    @Override
    public List<Map<String, Object>> getOrgTreeByOid(String oid) {
        List<Organization> list = this.organizationDAO.getFuzzyOrgByOid(oid);
        List<Map<String,Object>> orgTree = new ArrayList<Map<String,Object>>();
        for (Organization org : list) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("id", org.getOid());
            map.put("pId", org.getParentId());
//            map.put("name", org.getName() + "-" + org.getOid());
            map.put("name", org.getName());
            map.put("value", org.getOid());
            map.put("dName", org.getDistrict().getDistrictName());
            map.put("did", org.getDistrict().getDid());
            map.put("disabled", org.getDisabled());
            map.put("fullName", org.getFullName());
            map.put("open", true);
            orgTree.add(map);
        }
        return orgTree;
    }

    @Override
    public List<District> getDistrictListByParentId(String pid) {
        return organizationDAO.getFuzzyDistrictByParentId(pid);
    }
    
    /**
	 * @param pid
	 * @param did 广州市外的did
	 * @return
	 */
    @Override
    public List<District> getDistrictListByParentId(String pid,String did) {
    	return organizationDAO.getFuzzyDistrictByParentId(pid,did);
    }

}
